package com.example.androidproject.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class FileRepository {
    private SQLiteDatabase database;
    private FileDatabaseHelper dbHelper;

    public FileRepository(Context context) {
        dbHelper = new FileDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        dbHelper.createTable(database);
    }

    /**
     * 插入文件记录
     * @param fileName 文件名
     * @param timestamp 时间戳
     * @param sender 发送人
     */
    public void insertFile(String fileName, long timestamp, String sender) {
        ContentValues values = new ContentValues();
        values.put(FileDatabaseHelper.COLUMN_FILE_NAME, fileName);
        values.put(FileDatabaseHelper.COLUMN_TIMESTAMP, timestamp);
        values.put(FileDatabaseHelper.COLUMN_SENDER, sender);
        database.insert(FileDatabaseHelper.TABLE_FILES, null, values);
    }

    /**
     * 获取所有文件记录
     * @return 包含文件记录的列表
     */
    public List<FileRecord> getAllFiles() {
        List<FileRecord> files = new ArrayList<>();
        Cursor cursor = database.query(
                FileDatabaseHelper.TABLE_FILES,
                new String[]{FileDatabaseHelper.COLUMN_ID, FileDatabaseHelper.COLUMN_FILE_NAME, FileDatabaseHelper.COLUMN_TIMESTAMP, FileDatabaseHelper.COLUMN_SENDER},
                null, null, null, null, null
        );

        int idIndex = cursor.getColumnIndexOrThrow(FileDatabaseHelper.COLUMN_ID);
        int fileNameIndex = cursor.getColumnIndexOrThrow(FileDatabaseHelper.COLUMN_FILE_NAME);
        int timestampIndex = cursor.getColumnIndexOrThrow(FileDatabaseHelper.COLUMN_TIMESTAMP);
        int senderIndex = cursor.getColumnIndexOrThrow(FileDatabaseHelper.COLUMN_SENDER);

        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(idIndex);
                String fileName = cursor.getString(fileNameIndex);
                long timestamp = cursor.getLong(timestampIndex);
                String sender = cursor.getString(senderIndex);
                files.add(new FileRecord(id, fileName, timestamp, sender));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return files;
    }

    /**
     * 数据类，用于封装文件记录
     */
    public static class FileRecord {
        private int id;
        private String fileName;
        private long timestamp;
        private String sender;

        public FileRecord(int id, String fileName, long timestamp, String sender) {
            this.id = id;
            this.fileName = fileName;
            this.timestamp = timestamp;
            this.sender = sender;
        }
        public FileRecord(String fileName, long timestamp, String sender) {
            this.fileName = fileName;
            this.timestamp = timestamp;
            this.sender = sender;
        }

        public int getId() {
            return id;
        }

        public String getFileName() {
            return fileName;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public String getSender() {
            return sender;
        }

        @Override
        public String toString() {
            return "FileRecord{" +
                    "id=" + id +
                    ", fileName='" + fileName + '\'' +
                    ", timestamp=" + timestamp +
                    ", sender='" + sender + '\'' +
                    '}';
        }
    }
}
